package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.PageDto;
import com.club.football.dto.TeamSaveDto;
import com.club.football.entity.Club;
import com.club.football.entity.Player;
import com.club.football.entity.Team;
import com.club.football.mapper.TeamMapper;
import com.club.football.req.TeamPageReq;
import com.club.football.service.ClubService;
import com.club.football.service.PlayerService;
import com.club.football.service.TeamService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.FileUtils;
import com.club.football.utils.R;
import com.club.football.vo.TeamPageVo;
import com.club.football.vo.TeamVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.*;

/**
 * 球队服务实施
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements TeamService {
    @Resource
    private PlayerService playerService;
    @Resource
    private ClubService clubService;

    /**
     * 保存球队
     *
     * @param saveDto 球队将数据保存到
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> teamSave(TeamSaveDto saveDto) {
        Team one = lambdaQuery().ne(saveDto.getTeamId() != null, Team::getTeamId, saveDto.getTeamId())
                .eq(Team::getName, saveDto.getName()).one();
        if (one != null) {
            return R.failed("球队名已存在");
        }
        Team team = BeanUtils.copier(saveDto, new Team());
        Long userId = UserHolder.getUser().getUserId();
        String teamLogo = saveDto.getTeamLogo();
        team.setUpdateBy(userId);
        if (null != saveDto.getTeamId()) {
            if (StringUtils.isBlank(teamLogo)) {
                team.setTeamLogo(getById(saveDto.getTeamId()).getTeamLogo());
            }
            updateById(team);
            return R.ok(EDIT_OK_MSG);
        }
        team.setCreateBy(userId);
        if (StringUtils.isBlank(teamLogo)) {
            team.setTeamLogo(FileUtils.getImageUrl(DEFAULT_IMAGE));
        }
        save(team);
        return R.ok(SAVE_OK_MSG);
    }

    @Override
    public R<Page<TeamPageVo>> teamPage(PageDto<Team> pageDto, TeamPageReq teamPageReq) {
        lambdaQuery().like(StringUtils.isNotBlank(teamPageReq.getName()), Team::getName, teamPageReq.getName())
                .page(pageDto);
        List<Team> records = pageDto.getRecords();
        Page<TeamPageVo> teamPageVoPage = new Page<>();
        if (CollUtil.isNotEmpty(records)) {
            List<Club> clubList = clubService.list();
            Map<Long, String> clubMap = clubList.stream().collect(Collectors.toMap(Club::getClubId, Club::getClubName));
            List<TeamPageVo> pageRecords = records.stream().map(e -> toPageVo(e, clubMap)).collect(Collectors.toList());
            teamPageVoPage.setRecords(pageRecords);
        }
        teamPageVoPage.setTotal(pageDto.getTotal());
        return R.ok(teamPageVoPage, GET_INFO_OK);
    }

    @Override
    public R<TeamVo> teamInfo(Long id) {
        Team team = getById(id);
        TeamVo teamVo = BeanUtils.copier(team, new TeamVo());
        List<Player> playerList = playerService.lambdaQuery().eq(Player::getTeamId, id).list();
        teamVo.setPlayerList(playerList);
        String teamLogo = teamVo.getTeamLogo();
        teamVo.setTeamLogo(FileUtils.getUrl(teamLogo));
        return R.ok(teamVo, GET_INFO_OK);
    }

    private static TeamPageVo toPageVo(Team team, Map<Long, String> clubMap) {
        TeamPageVo teamPageVo = BeanUtils.copier(team, new TeamPageVo());
        teamPageVo.setClubName(clubMap.get(team.getClubId()));
        String teamLogo = teamPageVo.getTeamLogo();
        teamPageVo.setTeamLogo(FileUtils.getUrl(teamLogo));
        return teamPageVo;
    }
}
